package conversortemperatura.view;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JSlider;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

import conversortemperatura.application.service.ConversorService;
import conversortemperatura.application.service.impl.ConversorServiceImpl;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

public class ConversorTemperaturaPanel1 extends JPanel {
	private JTextField textField;
	private ConversorService conversorService;
	private JLabel lblResultado;
	
	/**
	 * Create the panel.
	 */
	public ConversorTemperaturaPanel1() {
		
		
		conversorService = new ConversorServiceImpl();
		
		setLayout(null);		
		
		inicializarComponetes();
		
		
		//Inicializar label resultado
		clicCalcular();
		
	}

	private void inicializarComponetes() {
		JLabel lblNewLabel = new JLabel("Temperatura \u00B0C :");
		lblNewLabel.setBounds(36, 45, 119, 14);
		add(lblNewLabel);
		
		JSlider slider = new JSlider();
		slider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				JSlider temp = (JSlider) e.getSource();
				textField.setText(  Integer.toString(temp.getValue()));
			}
		});		
		slider.setMajorTickSpacing(10);
		slider.setPaintTicks(true);
		slider.setPaintLabels(true);
		slider.setBounds(36, 100, 381, 71);
		add(slider);
		
		textField = new JTextField();
		textField.setEditable(false);
		textField.setBounds(165, 42, 86, 20);
		add(textField);
		textField.setColumns(10);
		textField.setText(Integer.toString(slider.getValue()));
		
		
		
		JLabel lblTemperaturaF = new JLabel("Temperatura F :");
		lblTemperaturaF.setBounds(36, 193, 93, 14);
		add(lblTemperaturaF);
		
		lblResultado = new JLabel("resultado");
		lblResultado.setBounds(139, 193, 86, 14);
		add(lblResultado);
		
		JButton btnCalcular = new JButton("CALCULAR");
		btnCalcular.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clicCalcular();
			}
		});
		btnCalcular.setBounds(282, 41, 135, 23);
		add(btnCalcular);
	}
	
	private void clicCalcular(){
		String farenheit = conversorService.centigradosToFarenheit(textField.getText());
		lblResultado.setText(farenheit);
	}

}
